package org.foreveross.que.web.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.foreveross.que.secret.SecretEncrypt;

@SuppressWarnings("serial")
public class ForwardFeekbackServlet extends HttpServlet {

	/**
	 * 密钥
	 */
	private String secretKey;
	/**
	 * 系统类型
	 */
	private String sysType;
	/**
	 * 生成口令有效时间（毫秒）
	 */
	private String availTime;
	/**
	 * session中username字段名称
	 */
	private String sessionUsername ;

	public void init() throws ServletException {
		ServletConfig config = this.getServletConfig();
		secretKey = config.getInitParameter("secretKey");
		sysType = config.getInitParameter("sysType");
		availTime = config.getInitParameter("availTime");
		sessionUsername = config.getInitParameter("sessionUsername");
	}

	
	  public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
		  try {
				String feekBackUrl = request.getParameter("feekBackUrl");
				if(feekBackUrl!=null&&!"".equals(feekBackUrl)){
					
//				String username = request.getParameter("username");
//				if(username==null||"".equals(username)){
				String username = getUsername(request);
//				}
				
				String mid = request.getParameter("mid");
				if(username!=null){
				String kouling = "username=" + username;
				if(mid!=null&&!"".equals(mid)){
					kouling += "&mid="+mid;
				}
				kouling += "&requestTime="+System.currentTimeMillis();
				kouling += "&availTime="+availTime;
				
				kouling = SecretEncrypt.encrypt(this.secretKey, kouling);
				if(feekBackUrl.indexOf("?")==-1){
					feekBackUrl += "?key="+kouling;
				}else{
					feekBackUrl += "&key="+kouling;
				}
				}else{
					if(mid!=null&&!"".equals(mid)){
						if(feekBackUrl.indexOf("?")==-1){
							feekBackUrl += "?vo.mid="+mid;
						}else{
							feekBackUrl += "&vo.mid="+mid;
						}
					}
				}
				feekBackUrl += "&vo.sysType="+sysType;
				response.sendRedirect(feekBackUrl);
				}else{
					super.doGet(request, response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  }


	private String getUsername(HttpServletRequest request) throws Exception {
		String username = "";
		if(sessionUsername.indexOf(".")==-1){
			 username = (String)request.getSession().getAttribute(sessionUsername);
		}else{
			String [] propertys = StringUtils.split(sessionUsername,".");
			Object tempObj = null;
			int i = 0;
			for(String pname:propertys){
				if(i==0){
					tempObj = request.getSession().getAttribute(pname);
				}else{
					tempObj = tempObj.getClass().getDeclaredMethod("get"+StringUtils.capitalize(pname)).invoke(tempObj);
				}
				if(tempObj==null){
					System.err.println("error: session '"+ sessionUsername + "' "+" '"+pname+"' is null!!" );
					break;
				}
				i++;
			}
			if(tempObj!=null)
			username = tempObj.toString();
		}
		return username;
	}
	  
	  public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException{
		  this.doGet(request, response);
	  }
	

}
